package cn.wolfcode.p2p.util;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 获取Session数据的工具类
 */
public abstract class UserContext {

        private static final String VERIFYCODEVO_IN_SESSION ="verifycodevo_in_session" ;
        private static final String LOGININDO_IN_SESSION ="logininfo" ;

        public  static HttpSession getSession(){
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                return request.getSession();
        }
        public static void setVerifyCodeVo(VerifyCodeVo vo){
                //这个对象没有request对象取,所以要强转
               /* RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();*/

                getSession().setAttribute(VERIFYCODEVO_IN_SESSION,vo);
        }

        public static VerifyCodeVo getVerifyCodeVo(){
                return (VerifyCodeVo) getSession().getAttribute(VERIFYCODEVO_IN_SESSION);
        }
        public static LoginInfo getLoginInfo(){
                return (LoginInfo) getSession().getAttribute(LOGININDO_IN_SESSION);
        }

        public static void setLoginInfo(LoginInfo user) {
                getSession().setAttribute(LOGININDO_IN_SESSION,user);
        }

        public static String  getIp() {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                return  request.getRemoteAddr();

        }
}
